package com.xdf.xzymanagementsystem.controller;

import com.xdf.xzymanagementsystem.entity.BillType;
import com.xdf.xzymanagementsystem.entity.OrderDetail;
import com.xdf.xzymanagementsystem.entity.OrderMainDTO;
import com.xdf.xzymanagementsystem.enums.MenuConstant;
import com.xdf.xzymanagementsystem.enums.OperateConstant;
import com.xdf.xzymanagementsystem.enums.ResponseEnum;
import com.xdf.xzymanagementsystem.enums.SeparatorConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.xdf.xzymanagementsystem.entity.OrderMain;
import com.xdf.xzymanagementsystem.service.OrderMainService;
import com.xdf.xzymanagementsystem.util.JsonResult;

/**
 * 注意要有依赖：
 *         <dependency>
 *             <groupId>io.springfox</groupId>
 *             <artifactId>springfox-swagger2</artifactId>
 *             <version>2.9.2</version>
 *         </dependency>
 *         <dependency>
 *             <groupId>io.springfox</groupId>
 *             <artifactId>springfox-swagger-ui</artifactId>
 *             <version>2.9.2</version>
 *         </dependency>
 * 同时入口类头上使用注解：@EnableSwagger2
 * @Description : 控制层，代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2025-3-21 18:43:41
 */
@Api(tags = "单据,主表,订单")
@CrossOrigin
@RestController
@RequestMapping("/orderMain")
public class OrderMainController {
    @Autowired
    private OrderMainService service;
    //=================以上是自动生成的代码，自定义代码请写在下面==================

    /**
     * 明细序时表
     * @param sdate
     * @param edate
     * @param flag
     * @return
     */
    @GetMapping("/selectAllDTO")
    @RequiresPermissions(MenuConstant.MENU101 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    public List<Map<String, Object>> selectAllDTO(@DateTimeFormat(pattern = "yyyy-MM-dd") Date sdate,
                                                  @DateTimeFormat(pattern = "yyyy-MM-dd") Date edate,
                                                  @RequestParam("flag") int flag) {
        System.out.println("sdate:" + sdate + ",edate:" + edate);
        List<Map<String, Object>> list = service.selectAllDTO(sdate, edate, flag);
        return list;
    }

    /**
     * 新增单据
     * @param record
     * @return
     */
    @ApiOperation(value = "新增单据", httpMethod = "POST")
    @RequiresPermissions(MenuConstant.MENU101 + SeparatorConstant.SEPARATOR + OperateConstant.ADD)
    @PostMapping("/insertDTO")
    public OrderMainDTO insertDTO(@RequestBody OrderMainDTO record) throws Exception {
        return service.insertDTO(record);
    }

    /**
     * 查询单据
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单据", httpMethod = "POST")
    @RequiresPermissions(MenuConstant.MENU101 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @PostMapping("/selectDTO/{id}")
    public JsonResult selectDTO(@PathVariable(value = "id")  Integer id){
        return JsonResult.ok(service.selectDTO(id), ResponseEnum.OK.getName());
    }

    /**
     * 修改单据
     * @param record
     * @return
     */
    @ApiOperation(value = "修改单据", httpMethod = "POST")
    @RequiresPermissions(MenuConstant.MENU101 + SeparatorConstant.SEPARATOR + OperateConstant.MODIFY)
    @PostMapping("/updateDTO")
    public OrderMainDTO updateDTO(@RequestBody OrderMainDTO record){
        return service.updateDTO(record);
    }

    /**
     * 批量作废单据
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量作废单据", httpMethod = "POST")
    @RequiresPermissions(MenuConstant.MENU101 + SeparatorConstant.SEPARATOR + OperateConstant.MODIFY)
    @PostMapping("/deleteByIds")
    public JsonResult deleteByIds(@RequestBody List<Integer> ids){
        service.deleteByIds(ids);
        return JsonResult.ok(ResponseEnum.OK.getName());
    }

    /**
     * 根据订单号查询单据
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "根据订单号查询单据", httpMethod = "POST")
    @RequiresPermissions(MenuConstant.MENU101 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @PostMapping("/selectByOrderNoDTO")
    public JsonResult selectByOrderNoDTO(@RequestParam("orderNo") String orderNo, @RequestParam("flag") int flag){
        return JsonResult.ok(service.selectByOrderNoDTO(orderNo, flag), ResponseEnum.OK.getName());
    }

    /**
     * 根据订单号查询单据
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "根据订单号查询单据", httpMethod = "POST")
    @RequiresPermissions(MenuConstant.MENU101 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @PostMapping("/selectByOrder")
    public List<OrderDetail> selectByOrderDetail(@RequestParam("orderNo") String orderNo){
        return service.selectByOrderDetail(orderNo);
    }

    /**
     * @Description : 查询订单号方法
     * @Return : java.util.List
     * @Author : 张柯
     * @Date : 2021-01-27 12:44:45
     */
    @ApiOperation(value = "查询订单号方法", httpMethod = "GET", notes = "查询订单号方法")
    @RequestMapping(value = "/listQuery", method = RequestMethod.GET)
    public List<Map<String, Object>> query() throws Exception {
        return service.listQuery();
    }

    /**
     * @Description : 查询订单号方法
     * @Return : java.util.List
     * @Author : 张柯
     * @Date : 2021-01-27 12:44:45
     */
    @ApiOperation(value = "查询订单号方法", httpMethod = "GET", notes = "查询订单号方法")
    @RequestMapping(value = "/listQueryAll", method = RequestMethod.GET)
    public List<Map<String, Object>> listQueryAll() throws Exception {
        return service.listQueryAll();
    }
    /**
     * 订单综合序时表
     * @param sdate
     * @param edate
     * @return
     */
    @GetMapping("/selectAllmain")
    @RequiresPermissions(MenuConstant.MENU101 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    public List<Map<String, Object>> selectAllmain(@DateTimeFormat(pattern = "yyyy-MM-dd") Date sdate,
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date edate,
                                                   @RequestParam("flag") int flag) {
        System.out.println("sdate:" + sdate + ",edate:" + edate);
        List<Map<String, Object>> list = service.selectAllmain(sdate, edate, flag);
        return list;
    }

    /**
     * 客户订单汇总表
     * @param sdate
     * @param edate
     * @return
     */
    @GetMapping("/selectAllsum")
    @RequiresPermissions(MenuConstant.MENU305 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    public List<Map<String, Object>> selectAllsum(@DateTimeFormat(pattern = "yyyy-MM-dd") Date sdate,
                                                  @DateTimeFormat(pattern = "yyyy-MM-dd") Date edate,
                                                  @RequestParam("flag") int flag) {
        System.out.println("sdate:" + sdate + ",edate:" + edate);
        List<Map<String, Object>> list = service.selectAllsum(sdate, edate, flag);
        return list;
    }

    /**
     * 客户订单明细表
     * @param orderNo
     * @return
     */
    @GetMapping("/selectAlldetail")
    @RequiresPermissions(MenuConstant.MENU305 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    public List<Map<String, Object>> selectAlldetail(@RequestParam String orderNo) {
        List<Map<String, Object>> list = service.selectAlldetail(orderNo);
        return list;
    }
}